//创建用户相关的
import {defineStore} from 'pinia'
import {apiGet} from "@/utils/api.ts";
import {sessionStorage} from "@/utils/storage.ts";

export const useUserStore = defineStore('user', {
    state: () => {
        return {
            username: '',
            nickname: '',
            avatar: '',
            accountBalance: 0,
            id: 0,
        }
    },
    //异步修改状态
    actions: {
        //请求用户个人信息
        async userInfo() {
            const {data} = await apiGet('/users/getById');
            //由于pinia|vuex存储数据其实利用js对象
            this.username = data.username;
            this.nickname = data.name;
            this.avatar = data.avatar;
            this.accountBalance = data.accountBalance;
            this.id = data.userId;
            return 'ok'
        },
        //退出登录
        async userLogout() {
            sessionStorage.remove('USER_TOKEN');
            this.username = ''
            this.nickname = ''
            this.avatar = ''
            this.accountBalance = 0
            this.id = 0
            return 'ok'
        }
    },
})
